package com.example.front.main.user;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.example.front.R;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.IOException;

import okio.BufferedSink;

public class UserFragment extends Fragment {

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_user, container, false);
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("Login", Context.MODE_PRIVATE);
        String account = sharedPreferences.getString("account", "");
        String username = sharedPreferences.getString("username", "");
        final TextView username_textView = root.findViewById(R.id.username_textView);
        final TextView account_textView = root.findViewById(R.id.account_textView);
        account_textView.setText(account);
        username_textView.setText(username);
        return root;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        final ImageView user_image = getActivity().findViewById(R.id.user_imageView);
        user_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), UserInformationActivity.class);
                startActivity(intent);
                getActivity().recreate();
            }
        });
        final TextView service_textView = getActivity().findViewById(R.id.service_textView);
        service_textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:18420510982"));
                Toast.makeText(getActivity(), "拉取手机通话工具中...", Toast.LENGTH_SHORT).show();
            }
        });
        final TextView setting_textView = getActivity().findViewById(R.id.setting_textView);
        setting_textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "打开设置界面中...", Toast.LENGTH_SHORT).show();
            }
        });
        final TextView exit_textView = getActivity().findViewById(R.id.exit_textView);
        exit_textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sharedPreferences = getActivity().getSharedPreferences("Login", Context.MODE_PRIVATE);
                sharedPreferences.edit().putBoolean("LoginBool", false).apply();
                getActivity().finish();
            }
        });
    }
}
